package com.hehe.boot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hehe.boot.pojo.Doctor;
import com.hehe.boot.pojo.DoctorHttp.DoctorRegisterRequest;
import com.hehe.boot.pojo.DoctorHttp.DoctorSetWorkTimeRequest;
import com.hehe.boot.pojo.Drug;
import com.hehe.boot.pojo.Result;
import com.hehe.boot.pojo.User;
import com.hehe.boot.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.print.Doc;
import java.util.HashMap;
import java.util.Map;


@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @PostMapping("/register")
    public Result<Map<String,Long>> Register(@RequestBody DoctorRegisterRequest doctorRegisterRequest){

        Long doctorId = doctorService.register(doctorRegisterRequest.getUsername(),
                doctorRegisterRequest.getPassword(),
                doctorRegisterRequest.getDoctor());
        Map<String,Long> map = new HashMap<>();
        map.put("DockerId",doctorId);
        return Result.success(map);
    }

    @GetMapping("/show")
    public Result<Doctor> ShowDetailByDoctorId(@RequestParam Long doctorId){
        return Result.success(doctorService.showDetailById(doctorId));
    }

    @PostMapping("/setWorkTime")
    public Result SetWorkTime(@RequestBody DoctorSetWorkTimeRequest request){
        if (request.getWorkStart().isAfter(request.getWorkEnd())){
            return Result.error("SetWorkTime error: Start time must after end time");
        }
        if(request.getWorkTimes()<=0){
            return Result.error("SetWorkTime error: Work time less than zero, seriously?");
        }
        Boolean flag = doctorService.setWorkTime(request.getDoctorId(),request.getWorkStart().plusHours(8),request.getWorkEnd().plusHours(8),request.getWorkTimes());

        if (flag == false){
            return Result.error("doctor setWorkTime error: Update work time fails");
        }

        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Doctor doctor){
        return Result.success(doctorService.updateDoctor(doctor));
    }

    @GetMapping("/find")
    public Result<IPage<Doctor>> findall(@RequestParam(required = false) String query,
                                      @RequestParam Integer page){
        if (query != null && !query.isEmpty()) {
            return Result.success(doctorService.getProPageLike(query,page));
        }
        return Result.success(doctorService.getProPage(page));
    }

    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Doctor drug){
        return Result.success(doctorService.insertDoctor(drug));
    }

    @PostMapping("/updateDoctor")
    public Result updateDoctor(@RequestBody Doctor drug){
        return Result.success(doctorService.updateDoctor(drug));
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Doctor drug){
        return Result.success(doctorService.deleteDoctor(drug));
    }
}
